package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServicelmpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //创建购物品对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //数据拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品是否已经在购物车当中
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {//购物车中存在
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else {//购物车中不存在
            //设置数量为1
            shoppingCart.setNumber(1);
            //设置创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //判断添加的是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if(dishId != null){//添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {//添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCartMapper.insert(shoppingCart);

        }
    }

    @Override
    public List<ShoppingCart> list(ShoppingCart shoppingCart) {
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();
        //调用Mapper方法
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //数据拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {
           // 获取购物车中的数据
            ShoppingCart cart = list.get(0);
            //当前购物车数据数量减一
            cart.setNumber(cart.getNumber() - 1);
            if (cart.getNumber() > 0) {
                shoppingCartMapper.update(cart);
            }else if (cart.getNumber() == 0) {
                shoppingCartMapper.delete(cart);
            }
        }
    }
}
